package grp.test.service.Impl;

import grp.test.common.ServerResponse;
import grp.test.dao.BookMapper;
import grp.test.dao.BookTypeMapper;
import grp.test.dao.BorrowMapper;
import grp.test.pojo.BookType;
import grp.test.pojo.Borrow;
import grp.test.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Date;
import java.math.BigDecimal;
/**
 * Created by Chloe on 2020/8/15.
 */
@Service("iPayService")
public class PayServiceImpl implements IPayService{
    /**
     *
     */
    @Autowired(required = false)
    private BorrowMapper borrowMapper;
    @Autowired(required = false)
    private BookTypeMapper bookTypeMapper;
    @Autowired(required = false)
    private BookMapper bookMapper;

    public BookTypeMapper getBookTypeMapper() {
        return bookTypeMapper;
    }

    public void setBookTypeMapper(BookTypeMapper bookTypeMapper) {
        this.bookTypeMapper = bookTypeMapper;
    }

    public BookMapper getBookMapper() {
        return bookMapper;
    }

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    Double delayMoneyFinal;//最终缴费金额
    int userid;//当前缴费用户ID
    int bookid;//当前缴费书籍ID

    @Override
    //获取逾期缴费信息
    public ServerResponse<Borrow> overduePay(Borrow key){
        Borrow borrow=borrowMapper.selectByPrimaryKey(key);
        if(key.getBookId() == null || key.getUserId()==null){
            return ServerResponse.createByErrorMessage("获取缴费信息失败！");
        }
        else{
            if(borrow.getBackDateReal() != null){
                //通过bookid查询typeid
                int bookType = bookMapper.queryBookType(key.getBookId());
                BookType type = bookTypeMapper.selectByPrimaryKey(bookType);
                System.out.println(type);
                userid = key.getUserId();
                bookid = key.getBookId();
                Date borBack = borrow.getBackDateReal();//实际还书日期
                Date borBorrow = borrow.getBorrowDate();//借书日期
                long day = (borBack.getTime() - borBorrow.getTime()) / (24 * 60 * 60 * 1000);//借书总时长，单位：天
                if (day <= type.getDefaultDate()) {
                    borrow.setDelayMoney(0.00);
                    return ServerResponse.createByErrorMessage("本次还书无需缴费");
                } else {
                    //double的计算不精确，BigDecimal可以提供精确的计算
                    BigDecimal realDay = new BigDecimal(Double.toString(day));
                    BigDecimal defaultDay = new BigDecimal(Double.toString(type.getDefaultDate()));
                    BigDecimal dpd = new BigDecimal(Double.toString(type.getDelayMoneyPerDay()));
                    BigDecimal delayMoney = realDay.subtract(defaultDay).multiply(dpd);
                    delayMoneyFinal =delayMoney.doubleValue();//将计算结果转换为double类型以更新数据库
                    borrow.setDelayMoney(delayMoneyFinal);
                    System.out.println(day);
                    System.out.println(type.getDefaultDate());
                    System.out.println(type.getDelayMoneyPerDay());
                    System.out.println(delayMoneyFinal);
                    return ServerResponse.createBySuccess("本次还书需缴滞纳金" + delayMoney + "元", borrow);
                }
            }
            else {
                return ServerResponse.createByErrorMessage("暂无缴费信息");
            }
        }
    }

    @Override
    //缴费操作
    public ServerResponse<Double> pay(Borrow key){
        if(key.getUserId()==userid && key.getBookId() == bookid){
            if(delayMoneyFinal == 0){
                return ServerResponse.createByErrorMessage("无需缴费");
            }
            return ServerResponse.createBySuccessMessage("本次还书已缴滞纳金"+delayMoneyFinal+"元");
        }
        return ServerResponse.createByErrorMessage("缴费失败！");
    }

    @Override
    //更新缴费信息
    public ServerResponse<Borrow> updatePayInfo(Borrow key){
        Borrow borrow = borrowMapper.selectByPrimaryKey(key);
        if( key.getUserId() == userid   && key.getBookId() == bookid){
            Borrow updateBorrow = new Borrow();
            updateBorrow.setUserId(userid);
            updateBorrow.setBookId(bookid);
            updateBorrow.setDelayMoney(delayMoneyFinal);
            updateBorrow.setBackDateSet(borrow.getBackDateSet());
            updateBorrow.setBackDateReal(borrow.getBackDateReal());
            updateBorrow.setBorrowDate(borrow.getBorrowDate());
            borrowMapper.updateByPrimaryKey(updateBorrow);
            return ServerResponse.createBySuccess("缴费信息已更新",updateBorrow);
        }
        else{
            return ServerResponse.createByErrorMessage("缴费信息更新失败！");
        }
    }
}
